package com.summli.basic.class05;

import java.util.Arrays;

/**
 * 基数排序
 */
public class Code04_RadixSort {

    public static void radixSort(int[] arr) {
        if(arr == null || arr.length<2){
            return;
        }
        int max= Integer.MIN_VALUE;
        for (int i = 0; i < arr.length; i++) {
            max = Math.max(max,arr[i]);
        }
        radixSort(arr,0,arr.length-1,maxbit(max));
    }

    private static int maxbit(int x) {
        int result =0;
        while(x!=0){
            x=x/10;
            result++;
        }
        return result;
    }

    public static void radixSort(int[] arr,int l,int r,int bit){
        // 使用额外的空间是等大的help，以及长度为10的arr
        // 基本思路：由小到大的位，依次统计位上出现的次数，之后cout[]依次叠加，从右向左依次放到help中，直至位数循环完

        int[] help = new int[r-l+1];
        for (int i = 1; i <= bit; i++) {
            int[] count = new int[10];
            for (int j = l; j <= r ; j++) {
                int digit = getDigit(arr[j],i);
                count[digit]++;
            }
            for (int j = 1; j < count.length; j++) {
                count[j] = count[j] + count[j-1];
            }
            for (int j = r; j >= l; j--) {
                int digit = getDigit(arr[j], i);
                help[--count[digit]] = arr[j];
            }
            for (int j = 0; j < arr.length; j++) {
                arr[j] = help[j];
            }
        }
        
    }

    //获取位数对应的数字
    private static int getDigit(int num,int bit) {
        return (num/(int)Math.pow(10,bit-1)) % 10;
    }
    // for test
    public static void comparator(int[] arr) {
        Arrays.sort(arr);
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random());
        }
        return arr;
    }

    // for test
    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // for test
    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100000;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            radixSort(arr1);
            comparator(arr2);
            if (!isEqual(arr1, arr2)) {
                succeed = false;
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");

        int[] arr = generateRandomArray(maxSize, maxValue);
        printArray(arr);
        radixSort(arr);
        printArray(arr);

    }

}
